package test;

import org.junit.Test;

import java.io.*;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;


/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/1/30 22:34
 * -@Content: 打印链表
 */
public class Demo {
    /**
     * 作者:Vpen
     * 描述:
     * 将文件读入到程序
     * */
    @Test
    public void test6() throws IOException {
        // 连接文件
        FileInputStream inputStream = new FileInputStream("h.txt");
        // 获取通道
        FileChannel channel = inputStream.getChannel();
        // 新建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(50);
        // 读取数据到缓冲区
        channel.read(buffer);
        // 切换到读模式
        buffer.flip();
        // remaining()是返回position和limit的差值
        String s = new String(buffer.array(), 0, buffer.remaining());
        System.out.println(s);
    }

    /**
     * 作者:Vpen
     * 描述:
     * 将信息写入文件中
     * */
    @Test
    public void test5() throws IOException {
        // 获取流
        RandomAccessFile accessFile = new RandomAccessFile("h.txt", "rw");
        // 通过流获取通道
        FileChannel channel = accessFile.getChannel();
        // 分配缓冲区大小
        ByteBuffer buffer = ByteBuffer.allocate(40);
        // 将字符串写入缓冲区
        buffer.put("Hello world".getBytes());
        // 需要重置position和limit的位置，
        buffer.flip();
        channel.write(buffer);
        channel.close();
    }

    /**
     * 作者:Vpen
     * 描述:
     * Buffer的方法测试
     * */
    @Test
    public void test4(){
        // allocate分配一个缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(40);
        show(buffer);


        // put往缓冲区添加数据
        String str = "New String好的";// 一个汉字三个字节 UTF-8
        buffer.put(str.getBytes());
        // 显示缓冲区的信息
        show(buffer);


        // flip切换到读模式 position调0 limit调到缓冲区当前长度
        buffer.flip();
        show(buffer);

        buffer.clear();
        show(buffer);
        byte b = buffer.get();
        // get数据读取
        System.out.println((char)b);
    }

    /**
     * 作者:Vpen
     * 描述:显示缓冲区的信息
     * */
    public void show(Buffer buffer){
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("-----------------");
    }

    @Test
    public void test1() throws IOException {
        //
        RandomAccessFile aFile = new RandomAccessFile("h.txt", "rw");
        // 造通道
        FileChannel channel = aFile.getChannel();
        // 造buffer
        ByteBuffer buffer = ByteBuffer.allocate(48);
        // 将通道读数据到buff
        int read = channel.read(buffer);
        while (read != -1){
            System.out.println(read);
            while (buffer.hasRemaining()){
                System.out.println((char) buffer.get());
            }
            buffer.clear();
            read = channel.read(buffer);
        }

        aFile.close();
    }

    @Test
    public void test2() throws IOException {
        // 造文件流对象
        RandomAccessFile accessFile = new RandomAccessFile("h.txt", "rw");
        // 获取通道对象
        FileChannel channel = accessFile.getChannel();
        // 要写入的字符串
        String newData = "new string to write";
        // 分配一个45大小的字节的buffer
        ByteBuffer buffer = ByteBuffer.allocate(45);
        // 清空buffer 防止有脏数据
        buffer.clear();
        // 将数据放入到buffer中
        buffer.put(newData.getBytes());

        // 切换读模式，读取buffer中的数据
        buffer.flip();

        while (buffer.hasRemaining()){
            // 将buff中的数据写入到通道中
            // 写(write)数据(buffer)到通道(channel)
            channel.write(buffer);
        }
    }
    @Test
    public void test3() throws IOException {
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        socketChannel.bind(new InetSocketAddress("127.0.0.1",80));
    }

    @Test
    public void test7(){
//        short i=1;
//        i= i+1;

    }
}



